package com.zhl.common.configuration;

import com.zhl.common.factory.FileFactory;
import com.zhl.common.properties.AliOssProperties;
import com.zhl.common.properties.ZhlIoProperties;
import com.zhl.common.service.AlibabaFileService;
import com.zhl.common.service.IFileService;
import com.zhl.common.service.TencentFileService;
import com.zhl.common.utils.COSUtils;
import com.zhl.common.utils.OSSUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @description 文件处理配置
 * @Author: z156940l@outlook.com
 * @className ServiceAutoConfiguration
 * @Date: 2024/7/21 15:35
 */
@Configuration
@ConditionalOnClass(FileFactory.class)
@EnableConfigurationProperties({ZhlIoProperties.class, AliOssProperties.class, TencentProperties.class})
@ComponentScan(basePackages = {"com.zhl.common.service"})
public class FileServiceAutoConfiguration {

    @Bean
    @ConditionalOnProperty(prefix = "zhl.io", name = "enable-ali-oss", havingValue = "true")
    @ConditionalOnBean(AliOssProperties.class)
    public OSSUtils ossUtils(AliOssProperties aliOssCos) {
        return new OSSUtils(aliOssCos);
    }

    @Bean
    @ConditionalOnProperty(prefix = "zhl.io", name = "enable-ali-oss", havingValue = "true")
    @ConditionalOnBean(OSSUtils.class)
    public IFileService aliOssFileService(OSSUtils ossUtils) {
        // 使用aliOssCos创建并返回AliOssFileService实例
        return new AlibabaFileService(ossUtils);
    }

    @Bean
    @ConditionalOnProperty(prefix = "zhl.io", name = "enable-tencent-cos", havingValue = "true")
    @ConditionalOnBean(TencentProperties.class)
    public COSUtils cosUtils(TencentProperties tencentCos) {
        return new COSUtils(tencentCos);
    }

    @Bean
    @ConditionalOnProperty(prefix = "zhl.io", name = "enable-tencent-cos", havingValue = "true")
    @ConditionalOnBean(COSUtils.class)
    public TencentFileService tencentFileService(COSUtils cosUtils) {
        return new TencentFileService(cosUtils);
    }

    @Bean
    @ConditionalOnBean(IFileService.class)
    public FileFactory fileFactory(List<IFileService> fileServices) {
        return new FileFactory(fileServices);
    }
}
